package com.pignest.hotel.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List; 

public class WhereCreator {
	private ArrayList<String> whereArrayList;
	private ArrayList<Object> parames; 
	private boolean where;

	public WhereCreator(boolean where) {
		this.where = where;
		whereArrayList = new ArrayList<String>();
		parames = new ArrayList<Object>();
	}

	public WhereCreator() {
		this.where = true;
		whereArrayList = new ArrayList<String>();
		parames = new ArrayList<Object>();
	}
  /**
   * @param where    加入到where的写法
   * @param value    判断是否加入where中 ,并且加入到where的？中
   */
	public void add(String where, Object value) {
		 this.add(where, value, value);
	}
	
	/**
	 * 
	 * @param where    加入到where的写法
	 * @param value    判断是否加入where中
	 * @param formate  加入到？的实际值
	 */
	public void add(String where, Object value,Object formate) {
		if (!"".equals(value) && value != null) {
			whereArrayList.add(where);
			parames.add(formate);
		}
	}

	public String getWhereString() {
		StringBuffer whereStr = new StringBuffer();
		for (int i = 0; i < whereArrayList.size(); i++) {
			if (i == 0 && this.where) {
				whereStr.append(" where");
			} else {
				whereStr.append(" and");
			}
			whereStr.append(" " + whereArrayList.get(i));
		}
		return whereStr.toString();
	}

	public Object[] getParame() {
		for(Object object:parames ){
			System.out.println("param==="+object);
		}
		return parames.toArray();
	}

	public String toString() {
		return getWhereString();
	}

	public String getInstr(Object usercode[]) {
		if(usercode==null) return null;
		String inStr = "";
		for (int i = 0; i < usercode.length; i++) {
			inStr = inStr + "'" + usercode[i] + "',";
		}
		if ("".equals(inStr) )
			return inStr;
		inStr = inStr.substring(0, inStr.length() - 1);
		return inStr;
	}

	public String getInstr(List<String> usercode) {
		String instr = "";
		for (int i = 0; i < usercode.size(); i++) {
			instr = instr + "'" + usercode.get(i) + "',";
		}
		if (instr.equals(""))
			return instr;
		instr = instr.substring(0, instr.length() - 1);

		return instr;
	}
	
	  public void formatAdd(String where,String value){ 
	    	if(value!=null&&!"".equals(value))
	    	   whereArrayList.add( String.format(where, value));
	    } 
	    public void formatAdd(String where,Long value){ 
	    	if(value!=null )
	    	   whereArrayList.add( String.format(where, value));
	    }    
	    public void formatAdd(String where,Double value){ 
	    	if(value!=null )
	    	   whereArrayList.add( String.format(where, value));
	    }   
	    public void formatAdd(String where,Date date){ 
	    	if(date==null )return;
	    	String dateStr=(new java.text.SimpleDateFormat("yyyy-MM-dd")).format(date);
	    	whereArrayList.add( String.format(where, dateStr));
	    }
	    public void formatAdd(String where,Date date,String format){ 
	    	if(date==null )return;
	    	String dateStr=(new java.text.SimpleDateFormat(format)).format(date);
	    	whereArrayList.add( String.format(where, dateStr));
	    }
	    

}
